package com.fresh.bus.chat.business;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.canoe.session.SessionRoleManager;
import com.fresh.bus.chat.ctrl.OpCodes;
import com.fresh.bus.chat.persist.UserDao;
import com.fresh.bus.chat.util.DateUtil;
import com.fresh.bus.chat.util.MessageUtils;

@Component
public class ExperienceService {
	private static final Logger log = LoggerFactory
			.getLogger(ExperienceService.class);
	private final Map<String, Integer> expMap;
	private StringRedisTemplate template;

	@Resource
	protected SessionRoleManager roleManager;
	@Resource
	protected WealthService wealthService;
	@Resource
	private SystemConfig config;
	@Resource
	private EnergyService energyService;

	@Autowired
	public ExperienceService(StringRedisTemplate template) {
		this.template = template;
		expMap = new HashMap<String, Integer>();
		initExpMap();
	}

	private void initExpMap() {
		expMap.put("user_register", +20); // 用户注册
		expMap.put("user_login", +5);// 用户登录
		expMap.put("write_feiji", +10);// 写纸飞机
		expMap.put("get_feiji", +10);// 收取飞机
		expMap.put("evaluate_feiji", +2);// 评估飞机

		expMap.put("follow_user", +3);// 关注他人
		expMap.put("write_msg", +5);// 主动留言
		expMap.put("keep_online", +5);// 保持在线

		expMap.put("talk_done", +10);// 完成谈话
		expMap.put("user_evelate", +5);// 真实性评估
		expMap.put("user_comment", +5);// 聊天备忘
		expMap.put("register_qq", +10);// 注册QQ号码
	}

	public void update(Session session, Object data, int opCode) {
		switch (opCode) {
		case OpCodes.LOGIN:
			loginUser(session, data);
			break;
		case OpCodes.WRITE_FEIJI:
			writeFeiji(session, data);
			break;
		case OpCodes.GET_FEIJI:
			getFeiji(session, data);
			break;
		case OpCodes.EVALUATE_FEIJI:
			evaluateFeiji(session, data);
			break;
		case OpCodes.GUANZHU_TA:
			followUser(session, data);
			break;
		case OpCodes.WRITE_MSG:
			writeMsg(session, data);
			break;
		case OpCodes.GET_TIME_REWARD:
			keepOnline(session, data);
			break;
		case OpCodes.USER_REPORT:
			evaluteUser(session, data);
			break;
		case OpCodes.USER_COMMENT:
			commentUser(session, data);
			break;
		case OpCodes.END_TALK:
			endTalk(session, data);
			break;
		case OpCodes.REGISTER_QQ:
			registerQQ(session, data);
			break;
		}

	}

	private void writeMsg(Session session, Object data) {
		int e = this.expMap.get("write_msg");
		sendResponse(session, e);
	}

	private void keepOnline(Session session, Object data) {
		int e = this.expMap.get("keep_online");
		sendResponse(session, e);
	}

	private void getFeiji(Session session, Object data) {
		int e = this.expMap.get("get_feiji");
		sendResponse(session, e);
	}

	private void followUser(Session session, Object data) {
		int e = this.expMap.get("follow_user");
		sendResponse(session, e);
	}

	private void writeFeiji(Session session, Object data) {
		int e = this.expMap.get("write_feiji");
		sendResponse(session, e);
	}

	private void evaluateFeiji(Session session, Object data) {
		Map<String, Object> map = (Map<String, Object>) data;
		int act = (Integer) map.get("act");
		int e = this.expMap.get("evaluate_feiji");
		sendResponse(session, e);
	}

	private void loginUser(Session session, Object data) {
		String id = session.getRole().getId();
		String uid = KeyUtils.uid(id);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		Date today = new Date();
		Date latestDay = DateUtil.convertStringToDate(ops.get("latestVisit"));
		int offsetDay = DateUtil.getDaysBetween(latestDay, today);

		Response response;
		// 首日注册初始化
		if (ops.get("isFirst") != null) {
			offsetDay = 1;
			ops.put("exp", String.valueOf(expMap.get("user_register")));
			response = new Response(OpCodes.FIRST_lOGIN);
			response.setData(true);
			session.send(response);
		}

		// 每日登录初始化
		if (offsetDay >= 1 || ops.get("isFirst") != null) {
			this.wealthService.getFlower(id, 2);
			int exp = Integer.valueOf(ops.get("exp"));
			int dp = Integer.valueOf(ops.get("dailyExp"));
			exp += (dp + expMap.get("user_login"));
			ops.put("exp", String.valueOf(exp));
			ops.put("dailyExp", "0");
			int e = this.expMap.get("user_login");
			sendResponse(session, e);
		} else {
			this.validAvailUpgrade(id);
		}

		if (offsetDay == 1) {
			this.wealthService.getFlower(id, 3);
			response = new Response(OpCodes.KEEP_lOGIN);
			response.setData(true);
			session.send(response);
		} else if (offsetDay > 1) {
			response = new Response(OpCodes.KEEP_lOGIN);
			response.setData(false);
			session.send(response);
		}
	}

	private void endTalk(Session session, Object data) {
		int e = this.expMap.get("talk_done");
		sendResponse(session, e);
	}

	private void registerQQ(Session session, Object data) {
		int e = this.expMap.get("register_qq");
		sendResponse(session, e);
	}

	private void evaluteUser(Session session, Object data) {
		int e = this.expMap.get("user_evelate");
		sendResponse(session, e);
	}

	private void commentUser(Session session, Object data) {
		int e = this.expMap.get("user_comment");
		sendResponse(session, e);
	}

	private void sendResponse(Session session, int e) {
		Response response;
		String id = session.getRole().getId();
		String uid = KeyUtils.uid(id);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		int dexp = Integer.valueOf(ops.get("dailyExp"));
		dexp += e;

		if (dexp > 100)
			dexp = 100; // 每日最高经验100
		ops.put("dailyExp", String.valueOf(dexp));

		this.validAvailUpgrade(id);
		response = new Response(OpCodes.SYN_EXPERIENCE);
		response.setData(e);
		session.send(response);
	}

	public void validAvailUpgrade(String id) {
		Session session = this.roleManager.getSession(id);
		String uid = KeyUtils.uid(id);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		int level = Integer.valueOf(ops.get("level"));
		Map<String, Object> cond = config.findLevel(level + 1);
		int needExp = (Integer) cond.get("exp");
		int exp = Integer.valueOf(ops.get("exp"));
		int dexp = Integer.valueOf(ops.get("dailyExp"));

		int income = Integer.valueOf(ops.get("income"));
		int needIncome = (Integer) cond.get("income");

		if (exp + dexp >= needExp && income >= needIncome) {
			Response response = new Response(OpCodes.AVAILABLE_UPGREADE);
			response.setData(true);
			if (session != null)
				session.send(response);
		}
	}
}
